
function [SL,SR,freq_pos,flag01]=mySTFT(x1,x2,fs,thre,tdur)

% t=(0:length(x1)-1)/fs;
%specify the window length and overlap size:
xlen=length(x1);  %length of the recorded signal
% tdur=1;        % time duration of each window
% tdur=0.08;        % time duration of each window
% tdur=0.04;        % time duration of each window
% tdur=0.02;        % time duration of each window
wlen=round(tdur*fs); %length of each window
hop=round((2/4)*wlen);  %the hop from one window to another (overlap size = wlen-hop )
win=hann(wlen,'periodic');  %hamming window of length wlen
% nfft=wlen*4;
nfft=wlen;
flag01 = 0;


coln_all=1+fix((xlen-wlen)/hop);  %number of time frames (windows)


index=0;
%     figure;
% coln_all
for coln=1:coln_all  %for each frame
%     coln
%     pause(0.1)
%     window the signal:
    xwl = x1(index+1:index+wlen).*win; %left
    xwr = x2(index+1:index+wlen).*win;
%         [f,XL]=myFT((0:length(xwl)-1)/fs,xwl); %dr ebel's fft function
%         [f,XR]=myFT((0:length(xwr)-1)/fs,xwr);
%     XL=fft(xwl);
%     XR=fft(xwr);
%     f = (1:length(XL))./length(XL).*fs;
%     %     [f,XL]=fft((0:length(xwl)-1)/fs,xwl); %dr ebel's fft function
%     %     [f,XR]=fft((0:length(xwr)-1)/fs,xwr);
%     SL(:,coln)=XL(end/2+1:end)';
%     SR(:,coln)=XR(end/2+1:end)';
    %freq_pos = f(end/2+1:end);
    
    index=index+hop;  %hop to the next window
%     thre = 0.05;
%     thre = 0.001;
    %     thre = 0;
    %     size(xwl)
    %     size(xwr)
    
    if max(max(abs([xwl' xwr'])))>thre
        %if max(abs([xwl' ]))>thre & max(abs([xwr' ]))>thre
        flag01(coln) = 1;
    else
        flag01(coln) = 0;
    end
end

% SL(1:10)
% %getting the phase of the left ear's STFT
[SL,freq_pos,t2] = spectrogram(x1,wlen,wlen-hop,nfft,fs,'yaxis');
% SL(1:10)
% pause
[SR,freq_pos,t2] = spectrogram(x2,wlen,wlen-hop,nfft,fs,'yaxis');




end
% =============================================


% % Investigate further what is going on here.. "scripts1-5 do not work with
% % above mySTFT
% 
% % 

% %

% function [SL,SR,freq_pos,flag01]=mySTFT(x1,x2,fs)
% 
% 
% 
% t=(0:length(x1)-1)/fs;
% 
% %specify the window length and overlap size:
% 
% xlen=length(x1);  %length of the recorded signal
% 
% tdur=0.08;        % time duration of each window
% 
% wlen=round(tdur*fs); %length of each window
% 
% hop=round((2/4)*wlen);  %the hop from one window to another (overlap size = wlen-hop )
% 
% win=hann(wlen,'periodic');  %hamming window of length wlen
% 
% nfft=wlen*4;
% 
% %flag01 = 0;
% 
% 
% 
% 
% 
% coln_all=1+fix((xlen-wlen)/hop);  %number of time frames (windows)
% 
% 
% 
% 
% 
% index=0;
% 
% %     figure;
% 
% for coln=1:coln_all  %for each frame
% 
%     %window the signal:
% 
%     xwl = x1(index+1:index+wlen).*win; %left
% % 
%     xwr = x2(index+1:index+wlen).*win;
% % 
% % %     [f,XL]=myFT((0:length(xwl)-1)/fs,xwl); %dr ebel's fft function
% % 
% % %     [f,XR]=myFT((0:length(xwr)-1)/fs,xwr);
% % 
%     XL=fft(xwl);
% % 
%     XR=fft(xwr);
% % 
%     f = (1:length(XL))./length(XL).*fs;
% % 
% % %     [f,XL]=fft((0:length(xwl)-1)/fs,xwl); %dr ebel's fft function
% % 
% % %     [f,XR]=fft((0:length(xwr)-1)/fs,xwr);
% % 
%     SL(:,coln)=XL(end/2+1:end)';
% 
%     SR(:,coln)=XR(end/2+1:end)';
% 
%     freq_pos = f(end/2+1:end);
% % 
% %     
% % 
%     index=index+hop;  %hop to the next window
% % 
%     thre = 0.05;
% 
%     if max(abs([xwl' xwr']))>thre
% 
%              %if max(abs([xwl' ]))>thre & max(abs([xwr' ]))>thre
% 
%         flag01(coln) = 1;
% 
%     else
% 
%         flag01(coln) = 0;
% 
%     end
% % 
% end
% % 
% % 
% % 
% % 
% % 
% % % % %getting the phase of the left ear's STFT
% % 
%  [SL,freq_pos,t2] = spectrogram(x1,wlen,wlen-hop,nfft,fs,'yaxis');
% 
% % 
% 
%  [SR,freq_pos,t2] = spectrogram(x2,wlen,wlen-hop,nfft,fs,'yaxis');
% % 
% % 
% % 
% % 
% % 
% % 
% % 
% % 
% % 
% % end